// https://www.luogu.com.cn/problem/P1434
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
typedef pair<int, pair<int, int>> node;
#define inf 0x3f3f3f3f
#define MAX_SIZE 10
#define MAXN 105

int dx[] = {0, -1, 0, 1, 0};
int dy[] = {0, 0, 1, 0, -1};

int n, m;
int a[MAXN][MAXN];
int f[MAXN][MAXN];

priority_queue<node, vector<node>, greater<node>> qe;

inline bool inside(int x, int y) {
    return 1 <= x && x <= n && 1 <= y && y <= m;
};

int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j)
            cin >> a[i][j], qe.push({a[i][j], {i, j}});

    int ans = 0;
    while (!qe.empty()) {
        int cnt = 0, x = qe.top().second.first, y = qe.top().second.second;
        qe.pop();
        for (int i = 1; i <= 4; ++i) {
            int nx = x + dx[i], ny = y + dy[i];
            if (inside(nx, ny) && a[nx][ny] < a[x][y])
                cnt = max(cnt, f[nx][ny]);
        }
        f[x][y] = cnt + 1;
        ans = max(ans, f[x][y]);
    }
    cout << ans;
}
